const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
// const unpluginVueDefineOptions = require('unplugin-vue-define-options/webpack');
module.exports = {
  publicPath: "./",
  css: {
    extract: false,
    loaderOptions: {

      // postcss: {
      //     plugins: [
      //         require('postcss-plugin-px2rem')({
      //             rootValue: process.env.VUE_APP_PAGE_FONT_SIZE * 1, //换算基数  
      //             unitPrecision: 5, //允许REM单位增长到的十进制数字。
      //             exclude: /(node_module)/,
      //             mediaQuery: false,  //（布尔值）允许在媒体查询中转换px。
      //             minPixelValue: 0 //设置要替换的最小像素值
      //         }),
      //     ]
      // },

      less: {
        lessOptions: {
          javascriptEnabled: true
        }
      }
    }
  },
  configureWebpack: config => {
    // config.plugins.push(unpluginVueDefineOptions());

    config.optimization.splitChunks = {
      chunks: 'all',
      minSize: 80 * 1024,
      maxInitialRequests: Infinity,

      cacheGroups: {
        vendor: {
          test: /[\\/]node_modules[\\/]/,
          name(module) {
            const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
            return `npm.${packageName.replace('@', '')}`;
          }
        },
        'echarts': {
          name: 'echarts',
          test: /[\\/]node_modules[\\/]echarts[\\/]/,
          priority: -10
        },
        'arco-design': {
          name: 'ant-design',
          chunks: 'all',
          test: /[\\/]node_modules[\\/]ant-design[\\/]/,
          priority: -10,
        },
        assets: {
          name: "lib.assets",
          priority: 20,
          test: /[\\/]src[\\/]_?assets(.*)/,
          chunks: 'all',
          enforce: true
        },
        commons: { // 其他同步加载公共包
          chunks: 'all',
          minChunks: 4,
          name: 'lib.commons',
          priority: 80,
        },

        // 'lodash': {
        //   name: 'lodash',
        //   test: /[\\/]node_modules[\\/]lodash[\\/]/,
        //   priority: -10
        // },
        'mockjs': {
          name: 'mockjs',
          test: /[\\/]node_modules[\\/]mockjs[\\/]/,
          priority: -10
        },
        // 's2': {
        //   name: 's2',
        //   test: /[\\/]node_modules[\\/]@antv[\\/]s2[\\/]/,
        //   priority: -10
        // }
      }
    };

    // config.optimization.runtimeChunk("single");

    config.optimization.minimizer.push(
      new UglifyJsPlugin({
        uglifyOptions: {
          output: {
            comments: false
          },
          compress: {
            drop_console: false, //删除所有调式带有console的
            drop_debugger: true,
            pure_funcs: [""] //删除console.log
          }
        }
      })
    );
    // 通过contenthash解决部署时cache问题
    config.output.filename = process.env.VUE_APP_ENV === 'prod' ? `js/[name].[contenthash:6].js` : 'js/[name].[hash:6].js'
    config.output.chunkFilename = process.env.VUE_APP_ENV === 'prod' ? `js/[name].[contenthash:6].js` : 'js/[name].[hash:6].js'
  }
};